<?php
/**
 * 编辑产品
 *
 * @category typecho
 * @package Widget
 * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
 * @license GNU General Public License 2.0
 * @version $Id$
 */

/**
 * 编辑产品组件
 *
 * @author qining
 * @category typecho
 * @package Widget
 * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
 * @license GNU General Public License 2.0
 */
class Widget_Contents_Order_Edit extends Widget_Abstract_Contents implements Widget_Interface_Do
{

	public function makeOrder(){

		if( $_SESSION['order_token']!=md5('gendiao') ){
			$this->response->throwJson(array('s' => 0, 'info'=> '请不要非法提交！'));
		}
		
		if( $this->request->get('cid')=="" ){
			$this->response->throwJson(array('s' => 0, 'info'=> '没有相关参数')); 
		}
		if( $this->request->get('name')=="" ){
			$this->response->throwJson(array('s' => 0, 'info'=> '姓名不能为空！')); 
		}
		if( $this->request->get('mail')=="" ){
			$this->response->throwJson(array('s' => 0, 'info'=> '邮箱不能为空！')); 
		}
		if( $this->request->get('tel')=="" ){
			$this->response->throwJson(array('s' => 0, 'info'=> '请留下您的电话！')); 
		}
		if( $this->request->get('add')=="" ){
			$this->response->throwJson(array('s' => 0, 'info'=> '地址不能为空！')); 
		}

		$data = array(
			'cid'  => $this->request->get('cid'),
			'name' => $this->request->get('name'),
			'mail' => $this->request->get('mail'),
			'tel'  => $this->request->get('tel'),
			'add'  => $this->request->get('add')
		);
		$this->db->query($this->db->insert('table.contents')
					->rows(array(
						'type'   =>   'order',
						'title'  =>   '产品-'. $this->request->get('cid'),
						'slug'   =>   md5(uniqid()),
						'parent' =>   $this->request->get('cid'),
						'created'=>   time(),
						'modified'=>   time(),
						'text'   =>   serialize($data)
					)));
					
		$this->response->throwJson(array('s'=>1, 'info'=>"订单添加成功，请耐心等待！\n\n我们的我工作人员会在第一时间与您联系。"));
	}
	

    /**
     * 删除文章
     *
     * @access public
     * @return void
     */
    public function deleteOrder()
    {
        $cid = $this->request->filter('int')->cid;
        $deleteCount = 0;
        $status = 'publish';

        if ($cid) {
            /** 格式化文章主键 */
            $posts = is_array($cid) ? $cid : array($cid);
            foreach ($posts as $post) {

                $condition = $this->db->sql()->where('cid = ?', $post);
                $row = $this->db->fetchRow($this->select()
                ->where('table.contents.type = ?', 'order')
                ->where('table.contents.cid = ?', $post)
                ->limit(1), array($this, 'push'));

                if ($this->isWriteable($condition) && $this->delete($condition)) {
                    /** 删除评论 */
                    $this->db->query($this->db->delete('table.comments')
                    ->where('cid = ?', $post));

                    $status = $this->status;

                    $deleteCount ++;
                }

                unset($condition);
            }
        }

        if ($this->request->isAjax()) {
            $this->response->throwJson($deleteCount > 0 ? array('code' => 200, 'message' => _t('订单已经被删除'))
            : array('code' => 500, 'message' => _t('没有订单被删除')));
        } else {
            /** 设置提示信息 */
            $this->widget('Widget_Notice')->set($deleteCount > 0 ? _t('订单已经被删除') : _t('没有订单被删除'), NULL,
            $deleteCount > 0 ? 'success' : 'notice');

            /** 返回原网页 */
            $this->response->redirect(Typecho_Common::url('manage-order.php', $this->options->adminUrl));
        }
    }
    /**
     * 绑定动作
     *
     * @access public
     * @return void
     */	 
    public function action()
    {
        $this->on($this->request->is('do=submit'))->makeOrder();
        $this->on($this->request->is('do=delete'))->deleteOrder();

       // $this->response->redirect($this->options->adminUrl);
    }
}
